package com.weimob.mcloud.wechatwork.customer.third.tag;

import com.alibaba.dubbo.config.annotation.Reference;
import com.weimob.mcloud.wechatwork.core.adapter.enums.channel.WechatWorkAppType;
import com.weimob.mcloud.wechatwork.core.adapter.pojo.dto.channel.request.QueryCorpTagDTO;
import com.weimob.mcloud.wechatwork.core.adapter.pojo.dto.channel.response.CorpTagGroup;
import com.weimob.mcloud.wechatwork.core.adapter.pojo.dto.channel.response.QueryCorpTagListRespDTO;
import com.weimob.mcloud.wechatwork.core.adapter.service.ChannelAdapterService;
import com.weimob.mcloud.wechatwork.core.entity.privatesphere.PrivateSphere;
import com.weimob.mcloud.wechatwork.core.enums.privateSphere.PrivateSphereEncryptCorpEnum;
import com.weimob.mcloud.wechatwork.core.service.privatesphere.PrivateSphereService;
import com.weimob.mcloud.wechatwork.customer.service.user.utils.SoaResponseUtil;
import com.weimob.soa.common.response.SoaResponse;
import com.weimob.zipkin.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.curator.shaded.com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * <P>
 *     企业微信官方渠道标签查询
 * </P>
 * @author junlong.lv
 * @version 1.0
 * @date 2021/11/1 8:25 下午
 */
@Component
@Slf4j
public class CorpTagThird {

    @Autowired
    private ChannelAdapterService channelAdapterService;

    @Reference
    private PrivateSphereService privateSphereService;

    /**
     * 企微助手产品id
     */
    @Value("${aewc.productId:203}")
    private Long aewcProductId;

    public CorpTagGroup getChannelTagGroup(PrivateSphere privateSphere, String type, String id){
        if (StringUtils.isBlank(id)){
            return null;
        }
        List<CorpTagGroup> corpTagGroups = queryCorpTagInfo(privateSphere, Lists.newArrayList(id), type);
        if (CollectionUtils.isNotEmpty(corpTagGroups)) {
            return corpTagGroups.get(0);
        }
        return null;
    }

    /**
     *
     * @param privateSphere
     * @param ids
     * @param way
     * @return
     */
    public List<CorpTagGroup> queryCorpTagInfo(PrivateSphere privateSphere, List<String> ids, String way) {

        QueryCorpTagDTO queryCorpTagDTO = new QueryCorpTagDTO();
        queryCorpTagDTO.setCorpId(privateSphere.getCorpId());
        queryCorpTagDTO.setWay(way);
        if (CollectionUtils.isNotEmpty(ids)) {
            queryCorpTagDTO.setIds(Lists.newArrayList(ids));
        }

        if (PrivateSphereEncryptCorpEnum.CIPHER.getCode().equals(privateSphere.getEncryptCorpId())) {
            queryCorpTagDTO.setTemplateId(privateSphere.getTemplateId());
            queryCorpTagDTO.setType(WechatWorkAppType.PROXIED_INTERNAL_APP.getType());

        } else {
            queryCorpTagDTO.setType(WechatWorkAppType.CONTACT.getType());
        }

        queryCorpTagDTO.setProviderIdentity(privateSphere.getChannelProviderIdentity());

        //查询企业微信官方标签信息
        SoaResponse<QueryCorpTagListRespDTO, Void> corpTagListRespDTOVoidSoaResponse = channelAdapterService.queryCorpTagList(queryCorpTagDTO);
        QueryCorpTagListRespDTO queryCorpTagListRespDTO = SoaResponseUtil.unpack(corpTagListRespDTOVoidSoaResponse);
        if (null == queryCorpTagListRespDTO) {
            return Lists.newArrayList();
        }
        return queryCorpTagListRespDTO.getCorpTagGroupList();
    }
}
